/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.task.JobUtil;
import org.springblade.modules.task.entity.BladeJob;
import org.springblade.modules.task.vo.BladeJobVO;
import org.springblade.modules.task.mapper.JobMapper;
import org.springblade.modules.task.service.IJobService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 任务表 服务实现类
 *
 * @author Blade
 * @since 2021-07-13
 */
@Service
public class JobServiceImpl extends BaseServiceImpl<JobMapper, BladeJob> implements IJobService {

    @Override
    public IPage<BladeJobVO> selectJobPage(IPage<BladeJobVO> page, BladeJobVO job) {
        return page.setRecords(baseMapper.selectJobPage(page, job));
    }

    @Override
    public boolean submit(BladeJob bladeJob) {
        if (bladeJob.getId() != null) {
//			更新任务前先停止任务
            stop(bladeJob.getId().toString());
        }
        bladeJob.setStatus(0);
        return saveOrUpdate(bladeJob);
    }

    @Override
    public boolean remove(String ids) {
        stop(ids);
        return deleteLogic(Func.toLongList(ids));
    }


    @Override
    public R start(String ids) {
        List<BladeJob> bladeJobs = listByIds(Func.toLongList(ids));
        if (bladeJobs == null || bladeJobs.size() == 0) {
            return R.fail("没有可执行的任务");
        }
        for (BladeJob bladeJob : bladeJobs) {
            if (bladeJob == null) {
                return R.fail("任务不存在");
            } else if (bladeJob.getStatus() == 1) {
                return R.fail("任务已启动");
            }
            if (!JobUtil.addJob(bladeJob)) {
                return R.fail("任务启动失败");
            }
            bladeJob.setStatus(1);
            updateById(bladeJob);
        }
        return R.success("任务启动成功");
    }

    @Override
    public R stop(String ids) {
        List<BladeJob> bladeJobs = listByIds(Func.toLongList(ids));
        if (bladeJobs == null || bladeJobs.size() == 0) {
            return R.fail("没有可执行的任务");
        }
        for (BladeJob bladeJob : bladeJobs) {
            if (bladeJob == null) {
                return R.fail("任务不存在");
            } else if (bladeJob.getStatus() == 0) {
                return R.fail("任务已停止");
            }
            if (!JobUtil.delJob(bladeJob)) {
                return R.fail("停止任务失败");
            }
            bladeJob.setStatus(0);
            updateById(bladeJob);
        }
        return R.success("任务停止成功");
    }

    @Override
    public void startAllTask() {
        QueryWrapper<BladeJob> queryWrapper = new QueryWrapper<BladeJob>();
        List<BladeJob> bladeJobs = list(queryWrapper.lambda().eq(BladeJob::getStatus, 1));
        for (BladeJob job : bladeJobs) {
            JobUtil.addJob(job);
        }
    }

}
